package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.rowset.CachedRowSet;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author iTaotao
 * @date 2023/7/25 17:36
 * @desciption: 分类实现类
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public PageResult queryPage(String name, String page, String pageSize, String type) {
        PageHelper.startPage(Integer.parseInt(page) , Integer.parseInt(pageSize));
       Page<Category> categories = categoryMapper.seletePage(name , type);
        long total = categories.getTotal();
        List<Category> result = categories.getResult();
        PageResult pageResult = new PageResult(total, result);
        return pageResult;
    }

    @Override
    public void addCategory(Category category) {
//        Long currentId = BaseContext.getCurrentId();
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(currentId);
//        category.setUpdateUser(currentId);
        categoryMapper.addCategory(category);

    }

    @Override
    public void removeCategoryById(String id) {
        categoryMapper.deleteCategoryById(id);
    }

    @Override
    public void updateCategoryById(Category category) {
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.updateCategoryById(category);
    }

    @Override
    public void updateStatusById(Long id, Integer status) {
        Category category = new Category();
        category.setId(id);
        category.setStatus(status);
        categoryMapper.updateStatusById(category);
    }

    @Override
    public List<Category> selectCategoryByType(Integer type) {
        Category category = new Category();
        category.setType(type);
        List<Category> categoryData = categoryMapper.selectCategoryByType(type);
       return categoryData;

    }
}
